﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Fields.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   The fields.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DbDiff.Common.DataAccess
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using Helper;
    using Model;

    /// <summary>
    /// The fields.
    /// </summary>
    public class Fields : DatabaseInfoExtended
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Fields"/> class.
        /// </summary>
        /// <param name="databaseObjectTypes">
        /// ThThe dataset name
        /// </param>
        /// <param name="databaseInfoBase">
        /// The databaseInfoBase
        /// </param>
        public Fields(DatabaseObjectTypes databaseObjectTypes, DatabaseInfoBase databaseInfoBase)
            : base(databaseObjectTypes, databaseInfoBase)
        {
        }

        /// <summary>
        /// The fill data set.
        /// </summary>
        /// <param name="schema">
        /// The schema.
        /// </param>
        /// <param name="table">
        /// The table.
        /// </param>
        /// <exception cref="Exception">
        /// </exception>
        public override void FillDataSet(string schema, string table)
        {
            this.DatabaseInfoBase.DatabaseDataSets.EnforceConstraints = true;

            // fill the original table without computed columns
            base.FillDataSet(schema, table);

            if (this.SqlVersion != SqlVersionInternal.Sql2000 && this.SqlVersion != SqlVersionInternal.Sql7)
            {
                return;
            }

            // fill computed columns
            string where = ((GetOption(this.DatabaseInfoBase.SqlObjectOptions, OptionNames.Schema).Trim().Length != 0)
                                ? "AND USER_NAME(o.uid) LIKE @Schema "
                                : string.Empty) +
                           ((GetOption(this.DatabaseInfoBase.SqlObjectOptions, OptionNames.Name).Trim().Length != 0)
                                ? " AND o.name LIKE @TableName  "
                                : string.Empty);

            string sqlCommand = this.DatabaseInfoBase.GetSqlCommand(DatabaseObjectTypes.ComputedColumns, this.SqlVersion, where);

            using (var connection = new SqlConnection(this.DatabaseInfoBase.ConnectionString))
            {
                var command = new SqlCommand(sqlCommand, connection);
                command.Parameters.Add("@Schema", SqlDbType.NVarChar, 400, "Schema").Value = schema;
                command.Parameters.Add("@TableName", SqlDbType.NVarChar, 400, "TableName").Value = table;
                command.CommandTimeout = this.QueryTimeOut;
                connection.Open();

                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        DbObjectsDs.FieldsRow row = this.DatabaseInfoBase.DatabaseDataSets.Fields.FindBySchemaNameTableNameName(reader.GetString(0), reader.GetString(1), reader.GetString(2));
                        row.ComputedText = row.ComputedText + reader.GetString(4);
                    }

                    reader.Close();
                }

                connection.Close();
            }
        }
    }
}